package com.test.Servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.entity.TestEntity;

public class ListTestServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Connection conn = null;
		Statement sta = null;
		ResultSet res = null;
		try {
			conn = conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/stest?serverTimezone=UTC&characterEncoding=utf8&amp", "root", "123456");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//声明一个ArrayList.用来存放类中的数据
		ArrayList<Object> list = new ArrayList<Object>();
		try {
			sta = conn.createStatement();
			String sql = "select * from tb_article_info";
			res = sta.executeQuery(sql);
			while (res.next()) {
				//建立了一个实体类,用来存放从数据库中拿到的数据
				TestEntity te = new TestEntity();
				te.setCaseid(res.getString("id"));
				te.setTitle(res.getString("title"));
				te.setName(res.getString("name"));
				te.setType(res.getString("type"));
				te.setText1(res.getString("text1"));
				te.setExpectResults(res.getString("expectResults"));
				te.setActualResults(res.getString("actualResults"));
				list.add(te);
			}
			//将list数据打包
			request.setAttribute("list", list);
			System.out.println(list);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//将list数据发送到.jap文件中
		request.getRequestDispatcher("tests.jsp").forward(request, response);
	}
 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
